local weilingy = fk.CreateSkill {

  name = "joy_weilingy",

  tags = {  },

}



weilingy:addEffect("viewas", {
  name = "joy_weilingy",
  prompt = "#weilingy-viewas",
  pattern = "slash,analeptic",
  interaction = function(self,player)
    local names, all_names = {} , {}
    local pat = Fk.currentResponsePattern
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id, true)
      if (card.trueName == "slash" or card.trueName == "analeptic") and
      not card.is_derived and not table.contains(all_names, card.name) then
        table.insert(all_names, card.name)
        local to_use = Fk:cloneCard(card.name)
        if not player:prohibitUse(to_use) then
          if pat == nil then
            if player:canUse(card) then
              table.insert(names, card.name)
            end
          else
            if Exppattern:Parse(pat):matchExp(card.name) then
              table.insert(names, card.name)
            end
          end
        end
      end
    end
    if #names == 0 then return false end
    return UI.ComboBox { choices = names, all_choices = all_names }
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = weilingy.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    use.extra_data = use.extra_data or {}
    use.extra_data.weilingy_user = player.id
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(weilingy.name) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedSkillTimes(weilingy.name) == 0
  end,
})

weilingy:addEffect(fk.CardUsing, {
  name = "#joy_weilingy_trigger",
  events = {fk.CardUsing,"fk.joy_weiling"},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return not player.dead and data.extra_data and data.extra_data.weilingy_user == player.id and data.card.color ~= Card.NoColor
    elseif event == "fk.joy_weiling" then
      return target == player and player:hasSkill(weilingy.name)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return true
    elseif event == "fk.joy_weiling" then
      return player.room:askForSkillInvoke(player,weilingy.name,nil,"你可以获得一张【水淹七军】")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local to
      local color = data.card:getColorString()
      for _, pid in ipairs(data:getAllTargets()) do
        to = room:getPlayerById(pid)
        if not to.dead then
          local mark = to:getTableMark( "@joy_weilingy-turn")
          if table.insertIfNeed(mark, color) then
            room:setPlayerMark(to, "@joy_weilingy-turn", mark)
            to:filterHandcards()
          end
        end
      end
    elseif event == "fk.joy_weiling" then
      player.room:setPlayerMark(player,"@joy_weilings",0)
      room:notifySkillInvoked(player, weilingy.name, "drawcard")
      local ids = getCards(room,"joy_shuiyan", 1)
      if #ids > 0 then
        room:obtainCard(player, ids[1], false, fk.ReasonPrey)
      end
    end
  end,
})
--[[睿智北冰洋
weilingy:addEffect("fk.joy_weiling", {
  name = "#joy_weilingy_trigger",
  events = {fk.CardUsing,"fk.joy_weiling"},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return not player.dead and data.extra_data and data.extra_data.weilingy_user == player.id and data.card.color ~= Card.NoColor
    elseif event == "fk.joy_weiling" then
      return target == player and player:hasSkill(weilingy.name)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return true
    elseif event == "fk.joy_weiling" then
      return player.room:askForSkillInvoke(player,weilingy.name,nil,"你可以获得一张【水淹七军】")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local to
      local color = data.card:getColorString()
      for _, pid in ipairs(data:getAllTargets()) do
        to = room:getPlayerById(pid)
        if not to.dead then
          local mark = to:getTableMark( "@joy_weilingy-turn")
          if table.insertIfNeed(mark, color) then
            room:setPlayerMark(to, "@joy_weilingy-turn", mark)
            to:filterHandcards()
          end
        end
      end
    elseif event == "fk.joy_weiling" then
      player.room:setPlayerMark(player,"@joy_weilings",0)
      room:notifySkillInvoked(player, weilingy.name, "drawcard")
      local ids = getCards(room,"joy_shuiyan", 1)
      if #ids > 0 then
        room:obtainCard(player, ids[1], false, fk.ReasonPrey)
      end
    end
  end,
})
--]]
weilingy:addEffect(fk.CardUsing, {
  refresh_events = {fk.CardUsing,fk.CardResponding},
  can_refresh =function (self, event, target, player, data)
    return target == player and player:hasSkill(weilingy.name) and player:getMark("@joy_weilings") < 7
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player,"@joy_weilings",1)
    if player:getMark("@joy_weilings") >= 7 then
      player.room.logic:trigger("fk.joy_weiling", player)
    end
  end
})
weilingy:addEffect(fk.CardResponding, {
  refresh_events = {fk.CardUsing,fk.CardResponding},
  can_refresh =function (self, event, target, player, data)
    return target == player and player:hasSkill(weilingy.name) and player:getMark("@joy_weilings") < 7
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player,"@joy_weilings",1)
    if player:getMark("@joy_weilings") >= 7 then
      player.room.logic:trigger("fk.joy_weiling", player)
    end
  end
})

weilingy:addEffect("filter", {
  name = "#joy_weilingy_filter",
  card_filter = function(self, to_select, player)
    if not table.contains(player.player_cards[Player.Hand], to_select.id) then return false end
    local mark = player:getTableMark("@joy_weilingy-turn")
    return table.contains(mark, to_select:getColorString())
  end,
  view_as = function(self, player,to_select)
    return Fk:cloneCard("slash", to_select.suit, to_select.number)
  end,
})

return weilingy